<!-- 矿山评价 -->
<template>
  <section class="content">
    <div>
      <vo-edit :data="chartData" :draggable="true" :direction='direction' :toggleCollapse="false"
      :exportButton="true" :toggleSiblingsResp="true" exportButtonName="导出" exportFilename="草地修复效果指标评价体系" ></vo-edit>
      <div id="edit-panel" class="view-state edit-container edit">
        <div class="item item-half">
          <div class="input-node-container">
            <label class="selected-node-group">被选节点：</label>
            <input type="text" id="selected-node" class="selected-node-group new-node" />
          </div>
          <div>
            <label>新节点：</label>
            <ul id="new-nodelist">
              <li>
                <input type="text" class="new-node" />
              </li>
            </ul>
          </div>
        </div>
        <div id="node-type-panel" class="radio-panel item">
          <input type="radio" name="node-type" id="rd-parent" value="parent" class />
          <label for="rd-parent">父节点</label>
          <input type="radio" name="node-type" id="rd-child" value="children" />
          <label for="rd-child">子节点</label>
          <input type="radio" name="node-type" id="rd-sibling" value="siblings" />
          <label for="rd-sibling">同级节点</label>
        </div>
        <div class="item">
          <el-button type="primary" @click="addNodes">增加</el-button>
          <el-button type="danger" @click="deleteNodes">删除</el-button>
          <!-- <el-button type="primary" @click="exportJSON">导出</el-button> -->
        </div>
      </div>
    </div>
    
  </section>
</template>
<script>
import { VoEdit } from 'vue-orgchart'

export default {
components: { VoEdit },
  data() {
    return {
      chartData: {
        name: '草地评价',
        children: [
          { name: '草地结构',
          children: [
            { name: '草地盖度'},
            { name: '土壤条件'},
            { name: '生态多样性'}
          ]},
          { name: '生态功能',
          children: [
            { name: '水源涵养量'},
            { name: '净初级生产力'}
          ]},
          { name: '工程措施',
          children: [
            { name: '生态护岸'}
          ]}
        ]
      },
        
    
    direction:"l2r",
      url:  'http://perrin.oss-cn-beijing.aliyuncs.com/cumtb/jixiaopingjia/1kuangshanpingjia.jpg',
      id: null,
      project: null,
      projectList: [],
      input: null,
      currentPage: 1,
      totalRecord: 0,
      totalPage: 0,
      tableData: [],
      detail: false,
      photo: [],
      formLabelWidth: "100px",
      riskRecord: {
        id: "",
        title: "111",
        brief: "222",
        photo: []
      },
      detailRecord:[{
        id: "1",
        title: "我是标题",
        brief: "我是日志",
        photo: "我是图片"
      }]
    };
  },
  methods: {
  

    //时间格式化
    dateFormat: function(row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },

    //OrgChart
    mountOrgchart() {
      this.$children.forEach((item) => {
        item.orgchart !== undefined ? this.orgchart = item.orgchart : null
      })
    },
    
    addNodes() {
      let chartContainer = document.getElementById('chart-container')
      let nodeVals = []

      Array.from(document.getElementById('new-nodelist').querySelectorAll('.new-node'))
        .forEach(item => {
          let validVal = item.value.trim()

          if (validVal) {
            nodeVals.push(validVal)
          }
        })
      let selectedNode = document.getElementById(document.getElementById('selected-node').dataset.node)

      if (!nodeVals.length) {
       this.$message({
            type: "warning",
            message: "请输入新节点的值！"
          });
        return
      }
      let nodeType = document.querySelector('input[name="node-type"]:checked')

      if (!nodeType) {
       this.$message({
            type: "warning",
            message: "请选择节点类型！"
          });
        return
      }
      if (nodeType.value !== 'parent' && !document.querySelector('.orgchart')) {
        this.$message({
            type: "warning",
            message: "请先创建根节点！"
          });
        return
      }
      if (nodeType.value !== 'parent' && !selectedNode) {
        this.$message({
            type: "warning",
            message: "请选择节点！"
          });
        return
      }
      if (nodeType.value === 'parent') {
        if (!chartContainer.children.length) {// if the original chart has been deleted
          this.orgchart = new OrgChart({
            'chartContainer': '#chart-container',
            'data': { 'name': nodeVals[0] },
            'parentNodeSymbol': 'fa-th-large',
            'createNode': function (node, data) {
              node.id = this.getId()
            }
          })
          this.orgchart.chart.classList.add('view-state')
        } else {
          this.orgchart.addParent(chartContainer.querySelector('.node'), { 'name': nodeVals[0], 'Id': this.getId() })
        }
      } else if (nodeType.value === 'siblings') {
        this.orgchart.addSiblings(selectedNode, {
          'siblings': nodeVals.map(item => {
            return { 'name': item, 'relationship': '110', 'Id': this.getId() }
          })
        })
      } else {
        let hasChild = selectedNode.parentNode.colSpan > 1

        if (!hasChild) {
          let rel = nodeVals.length > 1 ? '110' : '100'

          this.orgchart.addChildren(selectedNode, {
            'children': nodeVals.map(item => {
              return { 'name': item, 'relationship': rel, 'Id': this.getId() }
            })
          })
        } else {
          this.orgchart.addSiblings(closest(selectedNode, el => el.nodeName === 'TABLE').querySelector('.nodes').querySelector('.node'),
            { 'siblings': nodeVals.map(function (item) { return { 'name': item, 'relationship': '110', 'Id': this.getId() } })
            })
        }
      }
    },
    deleteNodes() {
      let sNodeInput = document.getElementById('selected-node')
      let sNode = document.getElementById(sNodeInput.dataset.node)

      if (!sNode) {
        this.$message({
            type: "warning",
            message: "请选择节点！"
          });
        return
      } else if (sNode === document.querySelector('.orgchart').querySelector('.node')) {
        if (!window.confirm('确定要删除吗?')) {
          return
        }
      }
      this.orgchart.removeNodes(sNode)
      sNodeInput.value = ''
      sNodeInput.dataset.node = ''
    },
    exportJSON() {
      let datasourceJSON = {}
      let ChartJSON = this.orgchart.getChartJSON()
      datasourceJSON = JSON.stringify(ChartJSON, null, 2)
      if(document.getElementsByTagName('code')[1]) {
        let code = document.getElementsByTagName('code')[1]
        code.innerHTML = datasourceJSON
      }
      return datasourceJSON
    },
    getId() {
      return (new Date().getTime()) * 1000 + Math.floor(Math.random() * 1001)
    }
    
  },
  mounted() {
    this.projectModel
      .projectGetAll()
      .then(res => {
        this.projectList = res.data;
      })
      .catch(err => {
        console.log(err.message);
      });
    let queryData = {
      currentPage: "1",
      pageSize: "10"
    };
    this.riskTabModel
      .queryList(queryData)
      .then(res => {
        this.totalRecord = res.data.totalRecord;
        this.totalPage = res.data.totalPage;
        this.tableData = res.data.list;
      })
      .catch(err => {
        console.log(err.message);
      });

      //OrgChart
      this.$nextTick(
      this.mountOrgchart()
    )
  }
};
</script>

<style scoped>
.edit{
text-align:center;

}
/* #chart{
  font-size:25px;
  height:30px;
  line-height: 30px;
  border-radius:4px 4px 4px 4px;
} */
.orgchart{
  font-size:25px;
  height:30px;
  line-height: 30px;
  border-radius:4px 4px 4px 4px;
}

#brief {
  white-space: normal;
  word-break: break-all;
  word-wrap: break-word;
}
.content {
  overflow-y: auto;
  padding-top: 10px;
  background-color: #ffffff;
  width: 100%;
  height: 100%;
}
#wrapper {
    width: 50%;
    margin: 0 auto
}

#wrapper li {
    margin-top: 20px
}

#wrapper a,
#wrapper span {
    font-size: 24px
}

#chart-container {
    position: relative;
    display: inline-block;
    top: 10px;
    left: 10px;
    height: 50%;
    width: calc(100% - 24px);
    border-radius: 5px;
    overflow: auto;
    overflow-x: hidden;
    text-align: center;
    font-family: Source Sans Pro, Helvetica Neue, Arial, sans-serif;
    font-size: 14px
}

.orgchart {
    display: inline-block;
    min-height: 100%;
    min-width: 100%;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-size: 10px 10px;
    border: 1px dashed transparent
}

.orgchart .node {
    width: 150px;
}

.orgchart .hidden,
.orgchart~.hidden {
    display: none
}

.orgchart div,
.orgchart div:after,
.orgchart div:before {
    -webkit-box-sizing: border-box;
    box-sizing: border-box
}

.orgchart.b2t {
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg)
}

.orgchart.l2r {
    -webkit-transform: rotate(-90deg) rotateY(180deg);
    transform: rotate(-90deg) rotateY(180deg);
    margin-top: 80px;
}

.orgchart .verticalNodes ul {
    list-style: none;
    margin: 0;
    padding-left: 18px;
    text-align: left
}

.orgchart .verticalNodes ul:first-child {
    margin-top: 3px
}

.orgchart .verticalNodes>td:before {
    content: "";
    border: 1px solid rgba(217, 83, 79, .8)
}

.orgchart .verticalNodes>td>ul>li:first-child:before {
    top: -4px;
    height: 30px;
    width: calc(50% - 2px);
    border-width: 2px 0 0 2px
}

.orgchart .verticalNodes ul>li {
    position: relative
}

.orgchart .verticalNodes ul>li:after,
.orgchart .verticalNodes ul>li:before {
    content: "";
    position: absolute;
    left: -6px;
    border-color: rgba(217, 83, 79, .8);
    border-style: solid;
    border-width: 0 0 2px 2px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box
}

.orgchart .verticalNodes ul>li:before {
    top: -4px;
    height: 30px;
    width: 11px
}

.orgchart .verticalNodes ul>li:after {
    top: 1px;
    height: 100%
}

.orgchart .verticalNodes ul>li:first-child:after {
    top: 24px;
    width: 11px;
    border-width: 2px 0 0 2px
}

.orgchart .verticalNodes ul>li:last-child:after {
    border-width: 2px 0 0
}

.orgchart.r2l {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg)
}

.orgchart>.spinner {
    font-size: 100px;
    margin-top: 30px;
    color: rgba(68, 157, 68, .8)
}

.orgchart table {
    border-spacing: 0;
    border-collapse: separate
}

.orgchart>table:first-child {
    margin: 20px auto
}

.orgchart td {
    text-align: center;
    vertical-align: top;
    padding: 0
}

.orgchart tr.lines .topLine {
    border-top: 2px solid #616161
}

.orgchart tr.lines .rightLine {
    border-right: 1px solid #616161;
    float: none;
    border-radius: 0
}

.orgchart tr.lines .leftLine {
    border-left: 1px solid #616161;
    float: none;
    border-radius: 0
}

.orgchart tr.lines .downLine {
    background-color: #616161;
    margin: 0 auto;
    height: 20px;
    width: 2px;
    float: none
}

.orgchart .node {
    display: inline-block;
    position: relative;
    margin: 0;
    padding: 3px;
    border: 2px dashed transparent;
    text-align: center;
    width: 130px
}

.orgchart.l2r .node,
.orgchart.r2l .node {
    width: 40px;
    height: 150px
}

.orgchart .node>.hazy {
    opacity: .2
}

.orgchart .node>.spinner {
    position: absolute;
    top: calc(50% - 15px);
    left: calc(50% - 15px);
    vertical-align: middle;
    font-size: 30px;
    color: rgba(68, 157, 68, .8)
}

.orgchart .node:hover {
    -webkit-transition: .5s;
    transition: .5s;
    cursor: default;
    z-index: 1
}

.orgchart .node.focused,
.orgchart .node:hover {
    background-color: rgba(238, 217, 54, .5)
}

.orgchart .ghost-node {
    position: fixed;
    left: -10000px;
    top: -10000px
}

.orgchart .ghost-node rect {
    fill: #fff;
    stroke: #bf0000
}

.orgchart .node.allowedDrop {
    border-color: rgba(68, 157, 68, .9)
}

.orgchart .node .title {
    text-align: center;
    font-size: 15px;
    font-weight: 300;
    width: 150px;
    height: 25px;
    line-height: 32px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    background-color: #42b983;
    color: #fff;
    border-radius: 4px 4px 0 0
}

.orgchart.b2t .node .title {
    -webkit-transform: rotate(-180deg);
    transform: rotate(-180deg);
    -webkit-transform-origin: center bottom;
    transform-origin: center bottom
}

.orgchart.l2r .node .title {
    -webkit-transform: rotate(-90deg) translate(-40px, -40px) rotateY(180deg);
    transform: rotate(-90deg) translate(-40px, -40px) rotateY(180deg);
    -webkit-transform-origin: bottom center;
    transform-origin: bottom center;
    width: 150px;
    height: 30px;
}

.orgchart.r2l .node .title {
    -webkit-transform: rotate(-90deg) translate(-40px, -40px);
    transform: rotate(-90deg) translate(-40px, -40px);
    -webkit-transform-origin: bottom center;
    transform-origin: bottom center;
    width: 120px
}

.orgchart .node .title .symbol {
    float: left;
    margin-top: 4px;
    margin-left: 2px
}

.orgchart .node .content {
    width: 100%;
    height: 20px;
    font-size: 11px;
    line-height: 18px;
    border: 1px solid #42b983;
    border-radius: 0 0 4px 4px;
    text-align: center;
    background-color: #fff;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap
}

.orgchart.b2t .node .content {
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg);
    -webkit-transform-origin: center top;
    transform-origin: center top
}

.orgchart.l2r .node .content {
    -webkit-transform: rotate(-90deg) translate(-40px, -40px) rotateY(180deg);
    transform: rotate(-90deg) translate(-40px, -40px) rotateY(180deg);
    -webkit-transform-origin: top center;
    transform-origin: top center;
    width: 120px
}

.orgchart.r2l .node .content {
    -webkit-transform: rotate(-90deg) translate(-40px, -40px);
    transform: rotate(-90deg) translate(-40px, -40px);
    -webkit-transform-origin: top center;
    transform-origin: top center;
    width: 120px
}

.orgchart .node .edge {
    font-size: 15px;
    position: absolute;
    color: rgba(68, 157, 68, .5);
    cursor: default;
    transition: .2s;
    -webkit-transition: .2s
}

.orgchart.noncollapsable .node .edge {
    display: none
}

.orgchart .edge:hover {
    color: #449d44;
    cursor: pointer
}

.orgchart .node .verticalEdge {
    width: calc(100% - 10px);
    width: -moz-calc(100% - 10px);
    left: 5px
}

.orgchart .node .topEdge {
    top: -4px
}

.orgchart .node .bottomEdge {
    bottom: -4px
}

.orgchart .node .horizontalEdge {
    width: 15px;
    height: calc(100% - 10px);
    height: -moz-calc(100% - 10px);
    top: 5px
}

.orgchart .node .rightEdge {
    right: -4px
}

.orgchart .node .leftEdge {
    left: -4px
}

.orgchart .node .horizontalEdge:before {
    position: absolute;
    top: calc(50% - 7px);
    top: -moz-calc(50% - 7px)
}

.orgchart .node .rightEdge:before {
    right: 3px
}

.orgchart .node .leftEdge:before {
    left: 3px
}

.orgchart .node .toggleBtn {
    position: absolute;
    left: 5px;
    bottom: -2px;
    color: rgba(68, 157, 68, .6)
}

.orgchart .node .toggleBtn:hover {
    color: rgba(68, 157, 68, .8)
}

.oc-export-btn {
    display: inline-block;
    position: absolute;
    right: 5px;
    bottom: 5px;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    color: #fff;
    background-color: #409eff;
    border: 1px solid transparent;
    border-color: #409eff;
    border-radius: 4px
}

.oc-export-btn:active,
.oc-export-btn:focus,
.oc-export-btn:hover {
    background-color: #409eff;
    border-color: #409eff
}

.orgchart~.mask {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 2;
    text-align: center;
    background-color: rgba(0, 0, 0, .3)
}

.orgchart~.mask .spinner {
    position: absolute;
    top: calc(50% - 54px);
    left: calc(50% - 54px);
    color: hsla(0, 0%, 100%, .8);
    font-size: 108px
}

.orgchart .node {
    -webkit-transition: all .3s;
    transition: all .3s;
    top: 0;
    left: 0
}

.orgchart .slide-down {
    opacity: 0;
    top: 40px
}

.orgchart.l2r .node.slide-down,
.orgchart.r2l .node.slide-down {
    top: 130px
}

.orgchart .slide-up {
    opacity: 0;
    top: -40px
}

.orgchart.l2r .node.slide-up,
.orgchart.r2l .node.slide-up {
    top: -130px
}

.orgchart .slide-right {
    opacity: 0;
    left: 130px
}

.orgchart.l2r .node.slide-right,
.orgchart.r2l .node.slide-right {
    left: 40px
}

.orgchart .slide-left {
    opacity: 0;
    left: -130px
}

.orgchart.l2r .node.slide-left,
.orgchart.r2l .node.slide-left {
    left: -40px
}

#edit-panel {
    position: relative;
    left: 10px;
    width: calc(100% - 40px);
    border-radius: 4px;
    float: left;
    margin-top: 20px;
    margin-bottom: 20px;
    padding: 10px 5px 10px 10px;
    font-size: 14px;
    line-height: 1.5;
    border-radius: 2px;
    color: rgba(0, 0, 0, .65);
    background-color: #fff
}

#edit-panel .btn-inputs {
    font-size: 24px
}

#edit-panel label {
    font-weight: 700
}

#edit-panel .new-node {
    height: 24px;
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    line-height: 1;
    border: 1px solid #d8dce5;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #5a5e66;
    display: inline-block;
    -webkit-transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
    transition: border-color .2s cubic-bezier(.645, .045, .355, 1)
}

#edit-panel .new-node:focus {
    border-color: #409eff;
    outline: none
}

#edit-panel .new-node:hover {
    border-color: #b4bccc
}

#edit-panel.edit-parent-node .selected-node-group {
    display: none
}

#btn-remove-input,
#chart-state-panel,
#selected-node {
    margin-right: 20px
}

#edit-panel button {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    border-radius: 4px;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none
}

#edit-panel.edit-parent-node button:not(#btn-add-nodes) {
    display: none
}

#edit-panel button:hover,
.edit-panel button:active,
.edit-panel button:focus {
    border-color: #409eff;
    -webkit-box-shadow: 0 0 10px #409eff;
    box-shadow: 0 0 10px #409eff
}

#new-nodelist {
    display: inline-block;
    list-style: none;
    margin-top: -2px;
    padding: 0;
    vertical-align: text-top
}

#new-nodelist>* {
    padding-bottom: 4px
}

.btn-inputs {
    vertical-align: sub
}

.btn-inputs:hover {
    text-shadow: 0 0 4px #fff
}

.radio-panel input[type=radio] {
    border: 1px solid #d8dce5;
    border-radius: 100%;
    cursor: pointer;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    display: inline-block;
    height: 14px;
    width: 14px;
    vertical-align: top
}

#edit-panel.view-state .radio-panel input[type=radio]+label {
    font-size: 14px;
    font-weight: 500;
    line-height: 1
}

#btn-add-nodes {
    margin-left: 20px
}
</style>